//Program by:
//Ben Schmidt - bjs778 - 11054018
//Mike Nimeck - mfn028 - 11053045
//Jeff Huang - jlh105 - 11050226

#include <stdlib.h>
#include <time.h>

#include <GLTools.h>
#include <GLMatrixStack.h>
#include <GLFrame.h>
#include <GLFrustum.h>
#include <GLGeometryTransform.h>
#include <StopWatch.h>

#include "main.h"
#include "utils.h"
#include "controller.h"

void windowToWorld(float &WIN_X, float &WIN_Y, float &WIN_Z){
    M3DMatrix44f worldView, window_to_world, device_to_window;
    M3DMatrix44f objectToScreen;
 
    const float* projection = view_frustum.GetProjectionMatrix();
        
    M3DVector4f screenCoords = {WIN_X, WIN_Y, 1.0f, 1.0f};
    
    view_frame.GetCameraMatrix(worldView, false);
        
    device_to_window[0] = WINDOW_WIDTH/2;
    device_to_window[1] = 0.0f;
    device_to_window[2] = 0.0f; 
    device_to_window[3] = 0.0f;
    device_to_window[4] = 0.0f;
    device_to_window[5] = WINDOW_HEIGHT/2;
    device_to_window[6] = 0.0f;
    device_to_window[7] = 0.0f;
    device_to_window[8] = 0.0f;
    device_to_window[9] = 0.0f;
    device_to_window[10] = -0.5f;
    device_to_window[11] = 0.0f;
    device_to_window[12] = WINDOW_WIDTH/2;
    device_to_window[13] = WINDOW_HEIGHT/2;
    device_to_window[14] = 0.5f;
    device_to_window[15] = 1.0f;
        
    m3dMatrixMultiply44(objectToScreen, projection, worldView);
    
    m3dMatrixMultiply44(device_to_window,  device_to_window, objectToScreen);
    
    m3dInvertMatrix44(window_to_world, device_to_window);
    
    M3DVector4f worldCoords;
    m3dTransformVector4(worldCoords, screenCoords, window_to_world);    
    

    WIN_X = -(worldCoords[0]/worldCoords[3])*(WIN_Z - (worldCoords[2]/worldCoords[3]));
    WIN_Y = -(worldCoords[1]/worldCoords[3])*(WIN_Z - (worldCoords[2]/worldCoords[3]));
}

void printControls(){
    	printf("\n##########\n#CONTROLS#\n##########\n\n");
    	if (particle_type == FIREWORKS){
		printf("CLICK......................Shoot firework\n");
		printf("SCROLL WHEEL...............Position firework closer/farther\n");
		printf("Q/A........................Increase/decrease firework Y speed\n");
		printf("W/S........................Increase/decrease firework X speed\n");		
		printf("E/D........................Increase/decrease firework Z speed\n");	
		printf("R/F........................Increase/decrease firework X scale\n");	
		printf("T/G........................Increase/decrease firework Y scale\n");	
		printf("Y/H........................Increase/decrease firework Z scale\n");
		printf("U/J........................Increase/decrease firework color fuzziness\n");
	}
	else if (particle_type == GRAVITY){
		printf("PAGE UP/PAGE DOWN..........Increase/decrease gravitational constant\n");
		printf("F2.........................Toggle gravity centering\n");
	}
	else
	{
		printf("Q/A........................Increase/decrease attempted distance from others\n");
		printf("W/S........................Increase/decrease positional spacing effort\n");
		printf("E/D........................Increase/decrease flock direction effort\n");
		printf("R/F........................Increase/decrease positional goal effort\n");
		printf("T/G........................Increase/decrease attempted color difference from others\n");
		printf("Y/H........................Increase/decrease color differentiation effort\n");
		printf("U/J........................Increase/decrease color direction effort\n");
		printf("I/K........................Increase/decrease color goal effort\n");
	}
	
	printf("UP/DOWN....................Make trail longer/shorter\n");
	printf("LEFT/RIGHT.................Swap shaders\n");
	printf("HOME/END...................Increase/decrease system speed\n");
	printf("1/2........................Increase/decrease base particle size\n");
	printf("3/4........................Increase/decrease depth divisor\n");
	printf("F11........................Toggle fullscreen mode\n");
	printf("SPACE......................Print controls\n");
	printf("\n");
}


